﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using AMS.UI.Forms;
using Spring.Context.Support;
using Spring.Context;
using AMS.DataAccess.Trademark.Interfaces;
using AMS.DataAccess.Trademark.BO;
using AMS.DataAccess.Trademark.Filters;
using AMS.Trademark.Management.Components;
using AMS.UI.Controls.Search;
using AMS.Trademark.Management.Helpers;

namespace AMS.Trademark.Management
{
    public partial class FrmRecvFileTaskConfig : BaseForm
    {
        public FrmRecvFileTaskConfig()
        {
            InitializeComponent();
            rfDetail.Visible = false;
            taskDetail.Visible = false;
            rfDetail.Dock = DockStyle.Fill;
            taskDetail.Dock = DockStyle.Fill;
            _searchingAdvTree = new SearchingAdvTree(tree, new SearchTextBox(txtSearchKeyword));
        }

        SearchingAdvTree _searchingAdvTree;

        /// <summary>
        /// 初始化
        /// </summary>
        public override void Init()
        {
            base.Init();
            //ApplyDelayLoadData();
            DelayLoadData();
        }

        protected override void DelayLoadData()
        {
            IApplicationContext context = ContextRegistry.GetContext();
            IRFTaskManager rfManager = (IRFTaskManager)context.GetObject("RFTask");
            DataTable data = rfManager.GetRFTasks(GetFilter());

            tree.Nodes.Clear();
            if (data != null && data.Rows.Count > 0)
            {
                // TODO: ad to tree list
                DevComponents.AdvTree.Node rfNode = null;
                DevComponents.AdvTree.Node taskNode = null;
                string lastRFIndex = string.Empty;
                for (int i = 0; i < data.Rows.Count; i++)
                {
                    DataRow row = data.Rows[i];
                    string rfIndex = (string)data.Rows[i]["RFIndex"];
                    if (rfIndex != lastRFIndex)
                    {
                        rfNode = new DevComponents.AdvTree.Node();
                        rfNode.Text = string.Format("{0} - {1}", row["RFIndex"], row["RFName"]);
                        rfNode.Tag = row["RFIndex"];

                        tree.Nodes.Add(rfNode);
                        rfNode.ImageIndex = 0;
                        lastRFIndex = (string)row["RFIndex"];
                    }

                    taskNode = new DevComponents.AdvTree.Node();
                    taskNode.Text = string.Format("{0} - {1}", row["TaskIndex"], row["TaskName"]);
                    taskNode.Tag = row["TaskIndex"];
                    taskNode.Cells.Add(new DevComponents.AdvTree.Cell(ObjectToString(row["StartType"])));
                    taskNode.Cells.Add(new DevComponents.AdvTree.Cell(GetDeadline(row["iAddMonth"], row["iAddDay"])));
                    taskNode.Cells.Add(new DevComponents.AdvTree.Cell(GetDeadline(row["oAddMonth"], row["oAddDay"])));

                    rfNode.Nodes.Add(taskNode);
                    taskNode.ImageIndex = 1;
                }

            }
        }

        private RFTaskFilter GetFilter()
        {
            return null;
        }

        private static string ObjectToString(object value)
        {
            if (value == null || value == DBNull.Value)
            {
                return string.Empty;
            }

            return value.ToString();
        }

        private string GetDeadline(object month, object day)
        {
            string smonth = "0";
            string sday = "0";
            if (month != null && month != DBNull.Value)
            {
                smonth = Convert.ToString(month);
            }

            if (day != null && day != DBNull.Value)
            {
                sday = Convert.ToString(day);
            }

            return smonth == "0" ? sday + "天" : smonth + "月" + sday + "天";
        }

        private void chkExpandAll_CheckedChanged(object sender, EventArgs e)
        {
            if (chkExpandAll.Checked)
            {
                tree.ExpandAll();
            }
            else
            {
                tree.CollapseAll();
            }
        }

        private void tree_SelectedIndexChanged(object sender, EventArgs e)
        {
            ProcessTreeSelectedIndexChange();
        }

        private void ProcessTreeSelectedIndexChange()
        {
            if (tree.Nodes.Count == 0 || tree.SelectedNode == null)
            {
                return;
            }
            if (tree.SelectedNode.Tag == null)
            {
                return;
            }

            string tag = tree.SelectedNode.Tag as string;
            NodeType nodeType = GetNodeType(tree.SelectedNode);
            IDetailControl selectedCtrl = null;
            if (nodeType == NodeType.RFNode)
            {
                rfDetail.Visible = true;
                taskDetail.BringToFront();
                taskDetail.Visible = false;
                selectedCtrl = rfDetail;
            }
            else if(nodeType == NodeType.TaskNode)
            {
                taskDetail.Visible = true;
                taskDetail.BringToFront();
                rfDetail.Visible = false;
                selectedCtrl = taskDetail;
            }

            if (selectedCtrl != null)
            {
                selectedCtrl.DataBind(tag);
                pnlSubTitle.Text = selectedCtrl.Title;
            }
        }

        private NodeType GetNodeType(DevComponents.AdvTree.Node node)
        {
            if (node.Parent == null)
            {
                return NodeType.RFNode;
            }
            else if (node.Parent.Parent == null)
            {
                return NodeType.TaskNode;
            }
            else
            {
                return NodeType.DocNode;
            }
        }

        /// <summary>
        /// 节点类型
        /// </summary>
        internal enum NodeType
        {
            Unknown,
            RFNode,
            TaskNode,
            DocNode,
        }

        private void cmbRuleType_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

    }
}
